<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${mapperPackage}.${oriClassName}${classSuffixMapper}">

	<resultMap id="baseResultMap" type="${viewBeanPackage}.${oriClassName}${classSuffixViewBean}">
<#if keyProperty??>
        <id column="${keyColumn}" property="${keyProperty}" ></id>

	<#list normalColumnPropertyCouples as columnPropertyCouple>
	    <result autocode="true"  column="${columnPropertyCouple.column}" property="${columnPropertyCouple.property}"/>
	</#list>

    <#list foreignKeys as foreignKey>
        <#list foreignKey.pkTable.columns as column>
        <result autocode="true" column="${column.columnAlias}" property="${column.propertyName}" ></result>
        </#list>
    </#list>



</#if>


	</resultMap>

<#if ascProperty??>
<insert id="add${oriClassName}" autocode="true"  keyProperty="${ascProperty}" useGeneratedKeys="true"  parameterType="${beanPackage}.${oriClassName}${classSuffixBean}" >
insert into `${tableName}` ( <#list colNamesWithoutAscProperty as colName> `${colName}` <#if colName_has_next>,</#if>  </#list>  )
values(<#list propertyNamesWithoutAscProperty as propertyName> ${r'#{'}${propertyName}${r'}'} <#if propertyName_has_next>,</#if>  </#list>)
</insert>
<#else>
<insert id="add${oriClassName}" autocode="true" parameterType="${beanPackage}.${oriClassName}${classSuffixBean}" >
insert into `${tableName}` ( <#list colNames as colName> `${colName}` <#if colName_has_next>,</#if>  </#list>  )
values(<#list propertyNames as propertyName>${r'#{'}${propertyName}${r'}'} <#if propertyName_has_next>,</#if>  </#list>)
</insert>

</#if>

<#if ascProperty??>
    <insert id="add${oriClassName}Selective" autocode="true"  keyProperty="${ascProperty}" useGeneratedKeys="true"  parameterType="${beanPackage}.${oriClassName}${classSuffixBean}" >



        insert into `${tableName}`
		(
        <trim suffixOverrides=",">
		<#list normalColumnPropertyCouples as columnPropertyCouple>
        <if test="${columnPropertyCouple.property} != null ">
            `${columnPropertyCouple.column}`,
        </if>


	</#list>
        </trim>
		 )
        values(
        <trim suffixOverrides=",">
		<#list normalColumnPropertyCouples as columnPropertyCouple>
            <if test="${columnPropertyCouple.property} != null   ">
			${r'#{'}${columnPropertyCouple.property}${r'}'},
            </if>

		</#list>
        </trim>
		)
    </insert>
<#else>
    <insert id="add${oriClassName}Selective" autocode="true" parameterType="${beanPackage}.${oriClassName}${classSuffixBean}" >
        insert into `${tableName}`
        (
        <trim suffixOverrides=",">
		<#list columnPropertyCouples as columnPropertyCouple>
            <if test="${columnPropertyCouple.property} != null   ">
                `${columnPropertyCouple.column}`,
            </if>

		</#list>
            </trim>

        )
        values(
        <trim suffixOverrides=",">
		<#list columnPropertyCouples as columnPropertyCouple>
            <if test="${columnPropertyCouple.property} != null   ">
			${r'#{'}${columnPropertyCouple.property}${r'}'},
            </if>

		</#list>
        </trim>

        )
    </insert>

</#if>




<#if keyProperty??>
<delete id="del${oriClassName}" autocode="true">
delete from `${tableName}` where ${keyColumn}=${r'#{'}${keyProperty}${r'}'}
</delete>
</#if>

<delete id="del${oriClassName}WithClause" autocode="true">
    delete from `${tableName}`

    where
    <trim prefix="" prefixOverrides="and|or">

    <#list columnPropertyCouples as columnPropertyCouple><if test="search.${columnPropertyCouple.property} != null   "> and `${columnPropertyCouple.column}`=${r'#{search.'}${columnPropertyCouple.property}${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>

    <#list columnPropertyCouples as columnPropertyCouple><if test="search.${columnPropertyCouple.property}Start != null   "> and `${columnPropertyCouple.column}`&gt;=${r'#{search.'}${columnPropertyCouple.property}Start${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>
    <#list columnPropertyCouples as columnPropertyCouple><if test="search.${columnPropertyCouple.property}End != null   "> and `${columnPropertyCouple.column}`&lt;=${r'#{search.'}${columnPropertyCouple.property}End${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>

    <#list columnPropertyCouples as columnPropertyCouple><if test="search.${columnPropertyCouple.property}List != null and search.${columnPropertyCouple.property}List.size()>0  "> and `${columnPropertyCouple.column}` in (	<foreach collection="search.${columnPropertyCouple.property}List" index="index" item="item" separator=","> ${r'#{'}item${r'}'}		</foreach> ) </if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>


    </trim>

</delete>


<#if keyProperty??>
<update id="update${oriClassName}" autocode="true">
update `${tableName}`

	<set>
		<#list normalColumnPropertyCouples as columnPropertyCouple>
			`${columnPropertyCouple.column}` = ${r'#{'}${columnPropertyCouple.property}${r'}'},

		</#list>


	</set>

 where `${keyColumn}`=${r'#{'}${keyProperty}${r'}'}
</update>
</#if>


<#if keyProperty??>
    <update id="update${oriClassName}Selective" autocode="true">
        update `${tableName}`

        <set>
			<#list normalColumnPropertyCouples as columnPropertyCouple>
            <if test="${columnPropertyCouple.property} != null   ">
			`${columnPropertyCouple.column}` = ${r'#{'}${columnPropertyCouple.property}${r'}'},
			</if>

			</#list>


        </set>

        where `${keyColumn}`=${r'#{'}${keyProperty}${r'}'}
    </update>


    <update id="increaseUpdate${oriClassName}Selective" autocode="true">
        update `${tableName}`

        <set>
            <#list normalColumnPropertyCouples as columnPropertyCouple>
                <if test="${columnPropertyCouple.property} != null   ">
                    `${columnPropertyCouple.column}` = `${columnPropertyCouple.column}` +  ${r'#{'}${columnPropertyCouple.property}${r'}'},
                </if>

            </#list>


        </set>

        where `${keyColumn}`=${r'#{'}${keyProperty}${r'}'}
    </update>




</#if>


    <update id="increaseUpdate${oriClassName}SelectiveWithClause" autocode="true">

        update `${tableName}`

        <set>
        <#list normalColumnPropertyCouples as columnPropertyCouple>
            <if test="bean.${columnPropertyCouple.property} != null   ">
                `${columnPropertyCouple.column}` = `${columnPropertyCouple.column}` + ${r'#{bean.'}${columnPropertyCouple.property}${r'}'},
            </if>

        </#list>


        </set>


        <trim prefix="where" prefixOverrides="and|or">

        <#list columnPropertyCouples as columnPropertyCouple><if test="search.${columnPropertyCouple.property} != null   "> and `${columnPropertyCouple.column}`=${r'#{search.'}${columnPropertyCouple.property}${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>

        <#list columnPropertyCouples as columnPropertyCouple><if test="search.${columnPropertyCouple.property}Start != null   "> and `${columnPropertyCouple.column}`&gt;=${r'#{search.'}${columnPropertyCouple.property}Start${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>
        <#list columnPropertyCouples as columnPropertyCouple><if test="search.${columnPropertyCouple.property}End != null   "> and `${columnPropertyCouple.column}`&lt;=${r'#{search.'}${columnPropertyCouple.property}End${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>

        <#list columnPropertyCouples as columnPropertyCouple><if test="search.${columnPropertyCouple.property}List != null and search.${columnPropertyCouple.property}List.size()>0  "> and `${columnPropertyCouple.column}` in (	<foreach collection="search.${columnPropertyCouple.property}List" index="index" item="item" separator=","> ${r'#{'}item${r'}'}		</foreach> ) </if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>


        </trim>


    </update>


    
    
    
    <update id="update${oriClassName}SelectiveWithClause" autocode="true">

        update `${tableName}`

        <set>
        <#list normalColumnPropertyCouples as columnPropertyCouple>
            <if test="bean.${columnPropertyCouple.property} != null   ">
                `${columnPropertyCouple.column}` = ${r'#{bean.'}${columnPropertyCouple.property}${r'}'},
            </if>

        </#list>


        </set>


        <trim prefix="where" prefixOverrides="and|or">

        <#list columnPropertyCouples as columnPropertyCouple><if test="search.${columnPropertyCouple.property} != null   "> and `${columnPropertyCouple.column}`=${r'#{search.'}${columnPropertyCouple.property}${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>

        <#list columnPropertyCouples as columnPropertyCouple><if test="search.${columnPropertyCouple.property}Start != null   "> and `${columnPropertyCouple.column}`&gt;=${r'#{search.'}${columnPropertyCouple.property}Start${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>
        <#list columnPropertyCouples as columnPropertyCouple><if test="search.${columnPropertyCouple.property}End != null   "> and `${columnPropertyCouple.column}`&lt;=${r'#{search.'}${columnPropertyCouple.property}End${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>

        <#list columnPropertyCouples as columnPropertyCouple><if test="search.${columnPropertyCouple.property}List != null and search.${columnPropertyCouple.property}List.size()>0  "> and `${columnPropertyCouple.column}` in (	<foreach collection="search.${columnPropertyCouple.property}List" index="index" item="item" separator=","> ${r'#{'}item${r'}'}		</foreach> ) </if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>


        </trim>


    </update>









<select id="list" autocode="true" parameterType="${searchBeanPackage}.${oriClassName}${classSuffixSearchBean}" resultMap="baseResultMap">


    select <#list foreignKeys as foreignKey> <#list foreignKey.pkTable.columns as column>`${foreignKey.pkTable.tableAlias}`.`${column.columnName}` as `${column.columnAlias}`,</#list> </#list>
<#list colNames as colName> `${primaryTableAliasTwo}`.`${colName}` <#if colName_has_next>,</#if>  </#list>
    from
    (

		select <#list colNames as colName> `${primaryTableAliasOne}`.`${colName}` <#if colName_has_next>,</#if>  </#list> from `${tableName}` as `${primaryTableAliasOne}`
		<trim prefix="where" prefixOverrides="and|or">

          <#list columnPropertyCouples as columnPropertyCouple><if test="${columnPropertyCouple.property} != null  "> and `${primaryTableAliasOne}`.`${columnPropertyCouple.column}`=${r'#{'}${columnPropertyCouple.property}${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>

          <#list columnPropertyCouples as columnPropertyCouple><if test="${columnPropertyCouple.property}Start != null  "> and `${primaryTableAliasOne}`.`${columnPropertyCouple.column}`&gt;=${r'#{'}${columnPropertyCouple.property}Start${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>
          <#list columnPropertyCouples as columnPropertyCouple><if test="${columnPropertyCouple.property}End != null  "> and `${primaryTableAliasOne}`.`${columnPropertyCouple.column}`&lt;=${r'#{'}${columnPropertyCouple.property}End${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>

          <#list columnPropertyCouples as columnPropertyCouple><if test="${columnPropertyCouple.property}List != null and ${columnPropertyCouple.property}List.size()>0  "> and `${primaryTableAliasOne}`.`${columnPropertyCouple.column}` in (	<foreach collection="${columnPropertyCouple.property}List" index="index" item="item" separator=","> ${r'#{'}item${r'}'}		</foreach> ) </if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>

			

		</trim>


       <choose>


           <when test="orderMap!=null and orderMap.size()&gt;0">
               order by
               <foreach collection="orderMap.keys" item="key" separator=","> `${primaryTableAliasOne}`.${r'${key} ${orderMap[key]} '} </foreach>
           </when>


           <otherwise>
           <#if keyProperty??>
               order by  `${primaryTableAliasOne}`.${keyProperty} desc
           </#if>               
           </otherwise>
       </choose>


		<if test="limitFlag==null||limitFlag==true"> limit ${r'${(page-1)*limitSize},${limitSize}'} </if>
    )as `${primaryTableAliasTwo}`
    <#list foreignKeys as foreignKey>
      left join ${foreignKey.pkTable.tableName} as `${foreignKey.pkTable.tableAlias}` on `${primaryTableAliasTwo}`.`${foreignKey.fkColumnName}`= `${foreignKey.pkTable.tableAlias}`.`${foreignKey.pkColumnName}`
    </#list>

	</select>
	
	<select id="listCount" autocode="true" parameterType="${searchBeanPackage}.${oriClassName}${classSuffixSearchBean}" resultType="java.lang.Integer">
				select count(1) from `${tableName}` as `${primaryTableAliasOne}`
		<trim prefix="where" prefixOverrides="and|or">
          <#list columnPropertyCouples as columnPropertyCouple><if test="${columnPropertyCouple.property} != null   "> and `${primaryTableAliasOne}`.`${columnPropertyCouple.column}`=${r'#{'}${columnPropertyCouple.property}${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>

          <#list columnPropertyCouples as columnPropertyCouple><if test="${columnPropertyCouple.property}Start != null   "> and `${primaryTableAliasOne}`.`${columnPropertyCouple.column}`&gt;=${r'#{'}${columnPropertyCouple.property}Start${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>
          <#list columnPropertyCouples as columnPropertyCouple><if test="${columnPropertyCouple.property}End != null   "> and `${primaryTableAliasOne}`.`${columnPropertyCouple.column}`&lt;=${r'#{'}${columnPropertyCouple.property}End${r'}'}</if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>

          <#list columnPropertyCouples as columnPropertyCouple><if test="${columnPropertyCouple.property}List != null and ${columnPropertyCouple.property}List.size()>0  "> and `${primaryTableAliasOne}`.`${columnPropertyCouple.column}` in (	<foreach collection="${columnPropertyCouple.property}List" index="index" item="item" separator=","> ${r'#{'}item${r'}'}		</foreach> ) </if> <#if columnPropertyCouple_has_next>${'\n'}</#if>          </#list>


		</trim>
	</select>

       <#if keyProperty??>
       <select id="getBy${keyPropertyInHumpStyleWithFirstUpper}" autocode="true"  resultMap="baseResultMap">

           select <#list foreignKeys as foreignKey> <#list foreignKey.pkTable.columns as column>`${foreignKey.pkTable.tableAlias}`.`${column.columnName}` as `${column.columnAlias}`,</#list> </#list>
        <#list colNames as colName> `${primaryTableAliasOne}`.`${colName}` <#if colName_has_next>,</#if>  </#list>
           from
              (
                select <#list colNames as colName> t.`${colName}` <#if colName_has_next>,</#if>  </#list> from `${tableName}` t where t.`${keyColumn}`=${r'#{'}${keyProperty}${r'}'}) as `${primaryTableAliasOne}`
           <#list foreignKeys as foreignKey>
               left join ${foreignKey.pkTable.tableName} as `${foreignKey.pkTable.tableAlias}` on `${primaryTableAliasOne}`.`${foreignKey.fkColumnName}`= `${foreignKey.pkTable.tableAlias}`.`${foreignKey.pkColumnName}`
           </#list>
       </select>
       </#if>


</mapper>
	
